package gateway.predicate;

import org.springframework.cloud.gateway.filter.GatewayFilter;
import org.springframework.cloud.gateway.handler.predicate.AbstractRoutePredicateFactory;
import org.springframework.cloud.gateway.handler.predicate.GatewayPredicate;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

import java.util.Collections;
import java.util.List;
import java.util.function.Predicate;

@Component
public class AbcRoutePredicateFactory extends AbstractRoutePredicateFactory<AbcRoutePredicateFactory.Config> {
    //自定义断言工厂类型名
    @Override
    public String name() {
        return "Aaa";
    }

    //断言中的配置名称
    public static class Config{
        private String name;

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }
    }

    public AbcRoutePredicateFactory() {
        //传递配置对象类型
        super(Config.class);

    }

    @Override
    public List<String> shortcutFieldOrder() {
        //绑定name值
        return Collections.singletonList("name");
    }

    @Override
    public Predicate<ServerWebExchange> apply(AbcRoutePredicateFactory.Config config) {
        return new GatewayPredicate() {
            //断言规则
            @Override
            public boolean test(ServerWebExchange serverWebExchange) {

                if("123".equals(config.getName())){
                    return true;
                }
                return false;
            }
        };
    }


}
